package canss.webservice.controls;


import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import canss.encoder.DBStringEncoder;
import canss.webservice.model.Feed;


import weblogic.jdbc.SQLExceptionWrapper;

import java.io.Serializable;

@ControlImplementation
public class FeedControlImpl implements FeedControl, Serializable
{
	private static final long serialVersionUID = 5030157996971788227L;

	@Control
	BusinessSystemDB businesssystemDB;

	public Boolean createFeedUri(String _pid, String _mid, String _uri)
			throws SQLExceptionWrapper
	{
		int chk = businesssystemDB.checkFeedUri(_pid, _mid, _uri);
		if(chk==0)	//doesn't duplicated? then
		{
			int ret = businesssystemDB.addFeedUri(DBStringEncoder.Server2DB(_pid),
					DBStringEncoder.Server2DB(_mid), DBStringEncoder
							.Server2DB(_uri), FeedControl.INITIALFEEDSTATUS);
			return ret > 0;
		}
		return false;
	}

	public Feed[] getFeedByMultiple(String _pid, String _mid)
			throws SQLExceptionWrapper
	{
		Feed[] feedlist = null;
		if (_pid != null && _mid != null)
		{
			feedlist = businesssystemDB.getFeedByPIDMID(DBStringEncoder.Server2DB(_pid), DBStringEncoder.Server2DB(_mid));
		} else if (_pid != null)
		{
			feedlist = businesssystemDB.getFeedByPID(DBStringEncoder.Server2DB(_pid));
		} else if (_mid != null)
		{
			feedlist = businesssystemDB.getFeedByMID(DBStringEncoder.Server2DB(_mid));
		}
		if (feedlist == null || feedlist.length == 0)
			return null;
		for (Feed feed : feedlist)
		{
			feed.setFEEDURI(DBStringEncoder.DB2Server(feed.getFEEDURI()));
			feed.setTBLMID_MID(DBStringEncoder.DB2Server(feed.getTBLMID_MID()));
			feed.setTBLPID_PID(DBStringEncoder.DB2Server(feed.getTBLPID_PID()));
			feed.setRULEID(DBStringEncoder.DB2Server(feed.getRULEID()));
		}
		return feedlist;
	}

	public Feed[] getFeedByUri(String _uri) throws SQLExceptionWrapper
	{
		if (_uri == null)
			return null;
		Feed[] feedlist = businesssystemDB.getFeedByUri(DBStringEncoder
				.Server2DB(_uri));
		if (feedlist == null || feedlist.length == 0)
			return null;
		for (Feed feed : feedlist)
		{
			feed.setFEEDURI(DBStringEncoder.DB2Server(feed.getFEEDURI()));
			feed.setTBLMID_MID(DBStringEncoder.DB2Server(feed.getTBLMID_MID()));
			feed.setTBLPID_PID(DBStringEncoder.DB2Server(feed.getTBLPID_PID()));
			feed.setRULEID(DBStringEncoder.DB2Server(feed.getRULEID()));
		}
		return feedlist;
	}

	public Boolean checkFeedExistenceByPIDMIDINDEX(String _pid, String _mid,
			String _index) throws SQLExceptionWrapper
	{
		if (_pid == null || _mid == null || _index == null)
			return null;
		int ret = businesssystemDB.getFeedExistence(_pid, _mid, _index);
		return ret > 0;
	}

	public Integer editFeedMID(String _pid, Integer _index, String _newmid)
			throws SQLExceptionWrapper
	{
		if (_pid == null || _index < 0 || _newmid == null)
			return null;
		return businesssystemDB.editFeedMIDByPIDIndex(DBStringEncoder.Server2DB(_pid), _index, DBStringEncoder.Server2DB(_newmid));
	}

	public Integer editFeedMID(String _pid, String _newmid)
			throws SQLExceptionWrapper
	{
		if (_pid == null || _newmid == null)
			return null;
		return businesssystemDB.editFeedMIDByPID(DBStringEncoder.Server2DB(_pid), DBStringEncoder.Server2DB(_newmid));
	}
	
	public Boolean deleteFeed(String _pid, String _mid, String _feeduri) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.deleteFeedByPIDMIDURI(DBStringEncoder.Server2DB(_pid), DBStringEncoder.Server2DB(_mid), DBStringEncoder.Server2DB(_feeduri));
		return ret>0;
	}
	
	public Boolean setFeedStatusByPID(String _pid, String _uri, Integer _status) throws SQLExceptionWrapper, RuntimeException
	{
		if(_status<0)
			throw new RuntimeException("strange status");
		int ret = businesssystemDB.setFeedStatus(DBStringEncoder.Server2DB(_pid), DBStringEncoder.Server2DB(_uri), _status);
		return ret>0;
	}

	public Feed[] getFeedStatusByPID(String _pid) throws SQLExceptionWrapper, RuntimeException
	{
		Feed[] feedlist = businesssystemDB.getFeedByPID(DBStringEncoder.Server2DB(_pid));
		if(feedlist==null && feedlist.length==0)
			return null;
		for(Feed feed : feedlist)
		{
			feed.setFEEDURI(DBStringEncoder.DB2Server(feed.getFEEDURI()));
			feed.setTBLMID_MID(DBStringEncoder.DB2Server(feed.getTBLMID_MID()));
			feed.setTBLPID_PID(DBStringEncoder.DB2Server(feed.getTBLPID_PID()));
			feed.setRULEID(DBStringEncoder.DB2Server(feed.getRULEID()));
		}
		return feedlist;
	}

	public Feed[] getFeedByStatus(Integer _feedstatus) throws SQLExceptionWrapper
	{
		Feed[] feedlist = businesssystemDB.getFeedByStatus(_feedstatus);
		if(feedlist==null || feedlist.length==0)
			return null;
		for(Feed feed : feedlist)
		{
			feed.setFEEDURI(DBStringEncoder.DB2Server(feed.getFEEDURI()));
			feed.setTBLMID_MID(DBStringEncoder.DB2Server(feed.getTBLMID_MID()));
			feed.setTBLPID_PID(DBStringEncoder.DB2Server(feed.getTBLPID_PID()));
			feed.setRULEID(DBStringEncoder.DB2Server(feed.getRULEID()));
		}
		return feedlist;
	}
	
	public int setFeedRulesByPIDURI(String _pid, String _uri, String _ruleid) throws SQLExceptionWrapper
	{
		return businesssystemDB.setFeedRulesByPIDURI(DBStringEncoder.Server2DB(_pid), DBStringEncoder.Server2DB(_uri), DBStringEncoder.Server2DB(_ruleid));
	}

	public Feed[] getEntireFeeds() throws SQLExceptionWrapper
	{
		Feed[] feedlist = businesssystemDB.getEntireFeeds();
		if(feedlist==null || feedlist.length==0)
			return null;
		for(Feed feed: feedlist)
		{
			feed.setFEEDURI(DBStringEncoder.DB2Server(feed.getFEEDURI()));
			feed.setTBLMID_MID(DBStringEncoder.DB2Server(feed.getTBLMID_MID()));
			feed.setTBLPID_PID(DBStringEncoder.DB2Server(feed.getTBLPID_PID()));
			feed.setRULEID(DBStringEncoder.DB2Server(feed.getRULEID()));
		}
		return feedlist;
	}
}